package com.lemon.reggie.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lemon.reggie.common.BusinessException;
import com.lemon.reggie.common.R;
import com.lemon.reggie.pojo.dto.AddSetMealDTO;
import com.lemon.reggie.pojo.dto.EditSetMealDTO;
import com.lemon.reggie.pojo.vo.GetSetMealVO;
import com.lemon.reggie.pojo.vo.SetMealVO;
import com.lemon.reggie.service.SetmealService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author Lemon
 * @since 2022-05-23
 */
@RestController
@RequestMapping("/setmeal")
@Validated
public class SetmealController {

    @Resource
    private SetmealService setmealService;

    @PostMapping
    public R<?> addSetMeal(@RequestBody @Valid AddSetMealDTO addSetMealDTO) {
        setmealService.addSetMeal(addSetMealDTO);
        return R.success("添加成功！");
    }

    @GetMapping("/page")
    public R<?> pageSetMeal(@RequestParam("page") Long currentPage,
                            @RequestParam("pageSize") Long pageSize,
                            @RequestParam(value = "name", required = false) String name) {
        IPage<SetMealVO> pages = setmealService.pageSetMeal(currentPage, pageSize, name);
        return R.success(pages);
    }

    @PostMapping("/status/{status}")
    public R<?> statusSetMeal(@PathVariable("status") Integer status,
                              @RequestParam("ids") List<Long> ids) {
        setmealService.statusSetMeal(status, ids);
        return R.success("修改成功！");
    }

    @DeleteMapping
    public R<?> deleteSetMeal(@RequestParam("ids") List<Long> ids) {
        String notNames = setmealService.deleteSetMeal(ids);
        if (StrUtil.isNotBlank(notNames)) {
            throw new BusinessException(notNames + "正在启售，不能删除！");
        } else {
            return R.success("删除成功！");
        }

    }

    @GetMapping("/{id}")
    public R<?> getSetMeal(@PathVariable("id") Long id) {
        GetSetMealVO getSetMealVO = setmealService.getSetMeal(id);
        return R.success(getSetMealVO);
    }

    @PutMapping
    public R<?> editSetMeal(@RequestBody EditSetMealDTO editSetMealDTO) {
        setmealService.editSetMeal(editSetMealDTO);
        return R.success("修改成功！");
    }

    @GetMapping("/list")
    public R<?> listSetMeal(@RequestParam("categoryId") @NotNull(message = "套餐分类id不能为空") Long categoryId,
                            @RequestParam("status") Integer status) {
        List<SetMealVO> setMealVOList = setmealService.listSetMeal(categoryId, status);
        return R.success(setMealVOList);
    }

}

